import os,sys

#try to get work dir by proc/pid/cmdline
fcmdline = None
argv0 = None
try:
    fcmdline = open('/proc'+'/'+str(os.getpid())+'/cmdline')
    cmdline = fcmdline.read()
    cmdstr = cmdline.split('\x00')
    argv0 = cmdstr[1]
    
except IOError,e:
    #should never happen
    print e.strerror
finally:
    if fcmdline:
        fcmdline.close()
    
#try to get work dir
workdir = argv0[ 0:argv0.rfind('/') ]
appname = argv0[ (argv0.rfind('/')+1) : len(argv0) ]
pidpath = workdir + '/' + appname + '.pid'
logpath = workdir + '/' + appname + '.log'

sys.stdout=open(logpath, 'w')
sys.stderr=open(logpath+'.err', 'w')

###############################################################################
import web

urls = (
    '/', 'index'
)

class index:
    def GET(self):
        return "Hello, world!"

    
if __name__ == "__main__":
    ##############################process pid###########################
    try:
        filePid = open(pidpath, 'rb')
        print 'web is running', filePid.read()
        filePid.close()
        exit(0)

    except IOError:
        pass


    filePid = open(pidpath, 'wb')
    filePid.write(str(os.getpid()))
    filePid.close()

    ####################################################################

    app = web.application(urls, globals())
    app.run()